package com.easyjop.Controller;

import com.easyjop.annotation.GlobalInterceptor;
import com.easyjop.annotation.VerifyParam;
import com.easyjop.entity.constants.Constants;
import com.easyjop.entity.dto.CreateImageCode;
import com.easyjop.entity.enums.VerifyRegexEnum;
import com.easyjop.entity.po.AppUserInfo;
import com.easyjop.entity.vo.ResponseVO;
import com.easyjop.exception.BusinessException;
import com.easyjop.service.AppUserInfoService;
import com.easyjop.utils.RedisUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/account")
public class AppAccountController extends ABaseController{
    private static final Integer CHECK_CODE_TYPE_REGISTER = 0;

    private static final Integer CHECK_CODE_TYPE_LOGIN = 1;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private AppUserInfoService appUserInfoService;
    @RequestMapping(value = "/checkCode")
    @GlobalInterceptor
    public void checkCode(HttpServletResponse response,
                          @VerifyParam(required = true) String deviceId,
                          @VerifyParam(required = true) Integer type) throws IOException {
        CreateImageCode vCode = new CreateImageCode(130, 35, 5, 10);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        String code = vCode.getCode();
        String redisKey = Constants.REDIS_KEY_CHECKCODE + deviceId + type;
        System.out.println("验证码："+code);
        redisUtils.cacheValue(redisKey, code, 10 * 60);
        vCode.write(response.getOutputStream());
    }
    @RequestMapping("/register")
    @GlobalInterceptor
    public ResponseVO register(
            HttpServletRequest request,
            @VerifyParam AppUserInfo appUserInfo,
            @VerifyParam(required = true) String checkCode,
            @VerifyParam(required = true, max = 32) String deviceId,
            @VerifyParam(max = 30) String deviceBrand) {
        String redisKey = Constants.REDIS_KEY_CHECKCODE + deviceId + CHECK_CODE_TYPE_REGISTER;
    try {
        String checkCodeRedis = (String) redisUtils.getValue(redisKey);

        if (!checkCode.equalsIgnoreCase(checkCodeRedis)) {
            throw new BusinessException("图片验证码不正确");
        }
        appUserInfo.setLastUseDeviceBrand(deviceBrand);
        appUserInfo.setLastUseDeviceId(deviceId);
        appUserInfo.setLastLoginIp(getIpAddr(request));
        appUserInfoService.regiser(appUserInfo);
        return getSuccessResponseVO("注册成功");
    }
    finally {
        redisUtils.removeValue(redisKey);
    }
    }
    @RequestMapping("/login")
    @GlobalInterceptor
    public ResponseVO login(HttpServletRequest request,
                            @VerifyParam(required = true) String email,
                            @VerifyParam(required = true) String password,
                            @VerifyParam(required = true) String checkCode,
                            @VerifyParam(required = true, max = 32) String deviceId,
                            @VerifyParam(max = 30) String deviceBrand) {
        String redisKey = Constants.REDIS_KEY_CHECKCODE + deviceId + CHECK_CODE_TYPE_LOGIN;
        try {
            String checkCodeRedis = (String) redisUtils.getValue(redisKey);
            if (!checkCode.equalsIgnoreCase(checkCodeRedis)) {
                throw new BusinessException("图片验证码不正确");
            }
            String token = appUserInfoService.login(email, password, getIpAddr(request), deviceId, deviceBrand);
            return getSuccessResponseVO(token);
        } finally {
            redisUtils.removeValue(redisKey);
        }
    }
    @RequestMapping("/autoLogin")
    @GlobalInterceptor
    public ResponseVO autoLogin(HttpServletRequest request, String token, @VerifyParam(required = true, max = 32) String deviceId,
                                @VerifyParam(max = 30) String deviceBrand) {
        String newToken = appUserInfoService.autoLogin(token, deviceId, deviceBrand, getIpAddr(request));
        return getSuccessResponseVO(newToken);
    }

}
